﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SettingsExtensions.cs" company="Mar3ek">
//   This code is licensed under the Mozilla Public License 2.0.
//   You can find the full license text at the following address:
//   https://winkee.codeplex.com/license
// </copyright>
// <summary>
//   Defines the SettingsExtensions type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WinKee.Common
{
    using System.Text.RegularExpressions;

    using Windows.Management.Deployment;
    using Windows.Storage;
    using Windows.UI;
    using Windows.UI.Core;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Media;

    /// <summary>
    /// The settings extension methods.
    /// </summary>
    public static class SettingsExtensions
    {
        private static readonly Regex ColorRegex = new Regex(
            "^#?(?<r>[0-9A-F]{2})(?<g>[0-9A-F]{2})(?<b>[0-9A-F]{2})$",
            RegexOptions.IgnoreCase);

        /// <summary>
        /// Reads the specified setting.
        /// </summary>
        /// <param name="container">
        /// The settings container.
        /// </param>
        /// <param name="key">
        /// The setting key.
        /// </param>
        /// <param name="defaultValue">
        /// The default value.
        /// </param>
        /// <typeparam name="T">
        /// The type of the setting to read.
        /// </typeparam>
        /// <returns>
        /// The retrieved setting value if the setting was found, otherwise the provided default value.
        /// </returns>
        public static T ReadSetting<T>(this ApplicationDataContainer container, string key, T defaultValue)
        {
            var value = container.Values[key];

            if (value == null)
            {
                return defaultValue;
            }

            return (T)value;
        }
    }
}
